/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datamodel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 *
 * @author Kvest
 */
public class Order
{
    public static final byte ORDER_1 = 1;
    
    public static final int STATE_NEW = 1;
    public static final int STATE_PERFORMING = 2;
    public static final int STATE_PERFORMED = 3;
    public static final int STATE_TAKEN = 4;
    public static final int STATE_MISSED = 5;
    public static final int STATE_CANCELLED = 6;
    
    //Данные класса
    protected int orderID;
    protected double fare;//Цена поездки
    protected int status;//Статус заказа,1 - новый,2 - выполняется,3 - выполненный,4 - заказ взят,но еще не подтвержден диспетчером,5 - пропущенный;6 - отмененный
    //Данные для автоудаления заказа
    protected long reciveTime; //Время получения заказа
    protected int N;           //Номер типа заказа
    protected boolean efirOrder;
    protected String folder;
    protected String orderType;
    protected boolean hasSuborders;
    
    private boolean preliminary;
    
    public Order(int orderID)
    {
        this.orderID = orderID;
        status = STATE_NEW;
        reciveTime = System.currentTimeMillis();
        N = 1;

        efirOrder = false;
        folder = "";
        orderType = "";
        preliminary = false;
        hasSuborders = true;
    }
    
    public Order(byte[] data) throws IOException
    { 
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);

        try
        {
            readFields(dis);
        }
        finally
        {
            try
            {
                dis.close();
                bais.close();
            }catch(Exception ex){}
        }

        preliminary = false;
        orderType = "";
        hasSuborders = true; 
    }
    
    public byte[] toByte()
    {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(result);
     
        try
        {
            writeFields(dos);

            return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                dos.close();
                result.close();
            }catch(Exception ex){}
        }
    }
    
    protected void readFields(DataInputStream dis) throws IOException
    {
        //int orderID;
        orderID = dis.readInt();

        //double fare;//Цена поездки
        fare = dis.readDouble();

        //int status
        status = dis.readInt();

        //folder
        folder = dis.readUTF();
        
        //Пропускаем тип заказа
        dis.read();
    }

    protected void writeFields(DataOutputStream dos) throws IOException
    {
        //int orderID;
        dos.writeInt(orderID);
        //double fare;//Цена поездки
        dos.writeDouble(fare);

        //int status
        dos.writeInt(status);

        //folder
        dos.writeUTF(folder);
        
        //Помечаем тип заказа
        dos.write(ORDER_1);
    }
    
    public String getFolder() 
    {
        return folder;
    }

    public void setFolder(String folder) 
    {
        this.folder = folder;
    }
        
    public void setOrderID(int id)
    {
        orderID = id;
    }

    public boolean isNewEfirOrder()
    {
        return (status == STATE_NEW && N == 3);
    }

    public int getN()
    {
            return N;
    }
	
    public void setN(int newN)
    {
        if ((newN>3)||(newN<1))
        {
            newN = 3;
        }
        N = newN;
        efirOrder = (N == 3);
    }
	
    public int getOrderID()
    {
            return orderID;
    }
	
    public void setFare(double newFare)
    {
            fare = newFare;
    }

    public double getFare()
    {
            return fare;
    }
    
    public int getStatus()
    {
        return status;
    }

    public void setStatus(int newStatus)
    {
        status = newStatus;
    }
    
    public boolean isDeleted(int[] T)
    {
        if ((status == STATE_NEW) && ((System.currentTimeMillis()-reciveTime) > T[N-1]))
        {
            return true;
        }
        else
        {
            return false;
        }	
    }
	
    public boolean isTooOld(int[] T)
    {
        return ((status == STATE_NEW) && ((System.currentTimeMillis() - reciveTime) > T[N-1])); 
//        if (status == STATE_NEW)
//        {
//            if ((System.currentTimeMillis() - reciveTime) > T[N-1])
//            {
//                if (N != 3)//Если заказ не из эфира - сохраняем как проигнорированный
//                {
//                    status = STATE_MISSED;
//                    return false;
//                }
//                else
//                {
//                    return true;
//                }
//            }
//            else
//            {
//                    return false;
//            }
//        }
//        else
//        {
//            return false;
//        }
    }

    public boolean isEfirOrder()
    {
        return efirOrder;
    }

    public boolean isPreliminary() 
    {
        return preliminary;
    }

    public void setPreliminary(boolean preliminary) 
    {
        this.preliminary = preliminary;
    }

    public String getOrderType() 
    {
        return orderType;
    }

    public void setOrderType(String orderType)
    {
        this.orderType = orderType;
    }

    public String getShortDescription()
    {
        return "";
    }
    
    public String getOrderFullDesc() 
    {      
        return "";
    }

    public String getOrderPrelimFullDesc() 
    {
        return "";
    }
    
    protected String formatPreffix(String value)
    {
        if (!value.equals(""))
        {
            return ("[" + value + "]");
        }
        else
        {
            return "";
        }
    }
    
    public boolean isAlien()
    {
        return false;
    }

    public boolean hasSuborders() {
        return hasSuborders;
    }

    public void setHasSuborders(boolean hasSuborders) {
        this.hasSuborders = hasSuborders;
    }
    
    public boolean isWithSum() {
        return (getFare() >= 0);
    }
}
